package com.nhatviet.webservice.helpers;

import java.util.List;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.nhatviet.webservice.models.FlickrPhoto;

public class XMLParserHandler extends DefaultHandler
{
	/* The start TAG of XML for detect the photos list */
	public static final String	XML_START								= "photos";
	/* The TAG of XML for detect a photo */
	public static final String	XML_START_PHOTO							= "photo";

	/* the current page */
	public static final String	XML_START_ATTRIBUTE_PAGE				= "page";
	/* the number of pages */
	public static final String	XML_START_ATTRIBUTE_NUM_PAGES			= "pages";
	/* the number of pages */
	public static final String	XML_START_ATTRIBUTE_NUM_PHOTO_PER_PAGE	= "perpage";
	/* the number of the photo */
	public static final String	XML_START_ATTRIBUTE_NUM_PHOTOS			= "total";

	/* the ID of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_ID			= "id";
	/* the owner of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_OWNER			= "owner";
	/* the secret number of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_SECRET		= "secret";
	/* the server of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_SERVER		= "server";
	/* the farm of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_FARM			= "farm";
	/* the title of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_TITLE			= "title";
	/* the is-public of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_IS_PUBLIC		= "ispublic";
	/* the is-friend of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_IS_FRIEND		= "isfriend";
	/* the is-family of the photo */
	public static final String	XML_START_PHOTO_ATTRIBUTE_IS_FAMILY		= "isfamily";

	/* The value of an element */
	// private String elementValue = null;
	/* The status OUT or On of an element */
	private boolean				isElementOn								= false;
	/* The index of the photos list */
	private int					index									= -1;

	/* The list of photos */
	private List<FlickrPhoto>	photosList								= null;
	/* The number of pages */
	public static int			numberOfPages							= 0;
	/* The number of photos */
	public static int			numberOfPhotos							= 0;
	/* The current page */
	public static int			currentPage								= 0;
	/* The number of photos per page */
	public static int			numberOfPhotosPerPage					= 0;

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		isElementOn = true;
		if (localName.equalsIgnoreCase(XML_START))
		{
			photosList = new Vector<FlickrPhoto>();
			int numberAttribute = attributes.getLength();
			for (int i = 0; i < numberAttribute; i++)
			{
				String attributeName = attributes.getLocalName(i);
				int value = 0;
				try
				{
					value = Integer.parseInt(attributes.getValue(i));
				} catch (Exception e)
				{
					value = 0;
				}
				if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_NUM_PAGES)) numberOfPages = value;
				else if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_NUM_PHOTO_PER_PAGE)) numberOfPhotosPerPage = value;
				else if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_NUM_PHOTOS)) numberOfPhotos = value;
				else if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_PAGE)) currentPage = value;
			}
		}
		else if (localName.equalsIgnoreCase(XML_START_PHOTO))
		{
			photosList.add(new FlickrPhoto());
			index++;
			int numberAttribute = attributes.getLength();
			for (int i = 0; i < numberAttribute; i++)
			{
				String attributeName = attributes.getLocalName(i);
				String attributeValue = attributes.getValue(i);
				if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_ID)) photosList.get(index).setId(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_FARM)) photosList.get(index).setFarm(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_FAMILY)) photosList.get(index).setIsFamily(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_FRIEND)) photosList.get(index).setIsFriend(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_PUBLIC)) photosList.get(index).setIsPublic(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_OWNER)) photosList.get(index).setOwner(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_SECRET)) photosList.get(index).setSecret(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_SERVER)) photosList.get(index).setServer(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_TITLE)) photosList.get(index).setTitle(attributeValue);
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException
	{
		if (isElementOn)
		{
			// elementValue = new String(ch, start, length).trim();
			isElementOn = false;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		isElementOn = false;
	}

	/**
	 * @return a list photos
	 */
	public List<FlickrPhoto> getPhotosList()
	{
		return photosList;
	}

	/**
	 * @return a specific list photos along attribute
	 */
	public List<String> getPhotosListAttribute(String attributeName)
	{		
		if(photosList == null || photosList.size() == 0) return null;
		List<String> listStrings = new Vector<String>();
		if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_ID))
		{
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getId());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_FARM)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getFarm());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_FAMILY)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getIsFamily());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_FRIEND)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getIsFriend());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_IS_PUBLIC)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getIsPublic());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_OWNER)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getOwner());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_SECRET)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getSecret());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_SERVER)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getServer());
			}
		}
		else if (attributeName.equalsIgnoreCase(XML_START_PHOTO_ATTRIBUTE_TITLE)) {
			for (FlickrPhoto flickrPhoto : photosList)
			{
				listStrings.add(flickrPhoto.getTitle());
			}
		}
		return listStrings;
	}

}
